import kotlinx.coroutines.*
import kotlin.test.Test

/**
 * @Author ZhouHang
 * @Date 24/7/15 周一
 */
class CoroutineTest {
    private val coroutineScope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
    @Test fun `Test withContext`() {
        coroutineScope.launch(Dispatchers.Default) {
            val order: String = withContext(Dispatchers.IO) {
                delay(2000)
                "New"
            }
            println("11111111111111 $order")
        }
        Thread.sleep(3000)
    }

    @Test fun `Test function call`() {
        coroutineScope.launch(Dispatchers.Default) {
            val order = withContext(Dispatchers.IO) {
                foo()
            }
            println("11111111111111 $order")
        }
    }

    private fun foo(): String {
        return "New"
    }

    @Test fun `Test Async`() {
        runBlocking {
            val job1 = async {
                delay(2000)
                2000
            }
            val job2 = async {
                delay(2000)
                3000
            }

            val v1 = job1.await()
            println(v1)
            val v2 = job2.await()
            println(v2)
        }
    }
}